﻿#include "MirButton.h"
#include "../MirSounds/SoundManager.h"

using namespace Client::MirSounds;

namespace Client::MirControls
{

    Color *MirButton::getFontColour() const
    {
        if (_label != nullptr && !_label->isDisposed())
        {
            return _label->getForeColour();
        }
        return Color::Empty;
    }

    void MirButton::setFontColour(Color *value)
    {
        if (_label != nullptr && !_label->isDisposed())
        {
            _label->setForeColour(value);
        }
    }

    int MirButton::getHoverIndex() const
    {
        return _hoverIndex;
    }

    void MirButton::setHoverIndex(int value)
    {
        if (_hoverIndex == value)
        {
            return;
        }
        _hoverIndex = value;
        OnHoverIndexChanged();
    }

    void MirButton::OnHoverIndexChanged()
    {
        if (HoverIndexChanged != nullptr)
        {
            HoverIndexChanged::Invoke(this, EventArgs::Empty);
        }
    }

    int MirButton::getIndex() const
    {
        if (!getEnabled())
        {
            return MirImageControl::getIndex();
        }

        if (_pressedIndex >= 0 && ActiveControl == this && MouseControl == this)
        {
            return _pressedIndex;
        }

        if (_hoverIndex >= 0 && MouseControl == this)
        {
            return _hoverIndex;
        }

        return MirImageControl::getIndex();
    }

    void MirButton::setIndex(int value)
    {
        MirImageControl::setIndex(value);
    }

    bool MirButton::getCenterText() const
    {
        return _center;
    }

    void MirButton::setCenterText(bool value)
    {
        _center = value;
        if (_center)
        {
            _label->setSize(getSize());
            _label->setDrawFormat(TextFormatFlags::HorizontalCenter | TextFormatFlags::VerticalCenter);
        }
        else
        {
            _label->setAutoSize(true);
        }
    }

    void MirButton::setPressedIndex(int value)
    {
        if (_pressedIndex == value)
        {
            return;
        }
        _pressedIndex = value;
        OnPressedIndexChanged();
    }

    int MirButton::getPressedIndex() const
    {
        return _pressedIndex;
    }

    void MirButton::OnPressedIndexChanged()
    {
        if (PressedIndexChanged != nullptr)
        {
            PressedIndexChanged::Invoke(this, EventArgs::Empty);
        }
    }

    void MirButton::OnSizeChanged()
    {
        MirImageControl::OnSizeChanged();

        if (_label != nullptr && !_label->isDisposed())
        {
            _label->setSize(getSize());
        }
    }

    void MirButton::setText(const std::string &value)
    {
        if (_label == nullptr || _label->isDisposed())
        {
            return;
        }
        _label->setText(value);
        _label->setVisible(!value.empty());
    }

    MirButton::MirButton()
    {
        setHoverIndex(-1);
        setPressedIndex(-1);
        setSound(SoundList::ButtonB);

        _label = new MirLabel();
        _label->setNotControl(true);
        _label->setParent(this);
    }

    void MirButton::Highlight()
    {
        Redraw();
        MirImageControl::Highlight();
    }

    void MirButton::Activate()
    {
        Redraw();
        MirImageControl::Activate();
    }

    void MirButton::Dehighlight()
    {
        Redraw();
        MirImageControl::Dehighlight();
    }

    void MirButton::Deactivate()
    {
        Redraw();
        MirImageControl::Deactivate();
    }

    void MirButton::Dispose(bool disposing)
    {
        MirImageControl::Dispose(disposing);

        if (!disposing)
        {
            return;
        }

        HoverIndexChanged->addListener("nullptr", nullptr);
        _hoverIndex = 0;

        if (_label != nullptr && !_label->isDisposed())
        {
            _label->Dispose();
        }
        _label = nullptr;

        PressedIndexChanged->addListener("nullptr", nullptr);
        _pressedIndex = 0;
    }
}
